package cn.iocoder.an.module.mold.controller.admin.devcategory;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.an.framework.common.pojo.PageParam;
import cn.iocoder.an.framework.common.pojo.PageResult;
import cn.iocoder.an.framework.common.pojo.CommonResult;
import cn.iocoder.an.framework.common.util.object.BeanUtils;
import static cn.iocoder.an.framework.common.pojo.CommonResult.success;

import cn.iocoder.an.framework.excel.core.util.ExcelUtils;

import cn.iocoder.an.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.an.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.an.module.mold.controller.admin.devcategory.vo.*;
import cn.iocoder.an.module.mold.dal.dataobject.devcategory.DevCategoryDO;
import cn.iocoder.an.module.mold.service.devcategory.DevCategoryService;

@Tag(name = "管理后台 - 设备分类")
@RestController
@RequestMapping("/mold/dev-category")
@Validated
public class DevCategoryController {

    @Resource
    private DevCategoryService devCategoryService;

    @PostMapping("/create")
    @Operation(summary = "创建设备分类")
    @PreAuthorize("@ss.hasPermission('mold:dev-category:create')")
    public CommonResult<Long> createDevCategory(@Valid @RequestBody DevCategorySaveReqVO createReqVO) {
        return success(devCategoryService.createDevCategory(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备分类")
    @PreAuthorize("@ss.hasPermission('mold:dev-category:update')")
    public CommonResult<Boolean> bigint(@Valid @RequestBody DevCategorySaveReqVO updateReqVO) {
        devCategoryService.updateDevCategory(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备分类")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mold:dev-category:delete')")
    public CommonResult<Boolean> deleteDevCategory(@RequestParam("id") Long id) {
        devCategoryService.deleteDevCategory(id);
        return success(true);
    }


    @GetMapping("/get")
    @Operation(summary = "获得设备分类")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mold:dev-category:query')")
    public CommonResult<DevCategoryRespVO> getDevCategory(@RequestParam("id") Long id) {
        DevCategoryDO devCategory = devCategoryService.getDevCategory(id);
        return success(BeanUtils.toBean(devCategory, DevCategoryRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备分类列表")
    @PreAuthorize("@ss.hasPermission('mold:dev-category:query')")
    public CommonResult<List<DevCategoryRespVO>> getDevCategoryList(@Valid DevCategoryListReqVO listReqVO) {
        List<DevCategoryDO> list = devCategoryService.getDevCategoryList(listReqVO);
        return success(BeanUtils.toBean(list, DevCategoryRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备分类 Excel")
    @PreAuthorize("@ss.hasPermission('mold:dev-category:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDevCategoryExcel(@Valid DevCategoryListReqVO listReqVO,
              HttpServletResponse response) throws IOException {
        List<DevCategoryDO> list = devCategoryService.getDevCategoryList(listReqVO);
        // 导出 Excel
        ExcelUtils.write(response, "设备分类.xls", "数据", DevCategoryRespVO.class,
                        BeanUtils.toBean(list, DevCategoryRespVO.class));
    }

}